package DTO;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import database.DatabaseHelper;

public class CorrectAnswerDTO {
	
	//Properties
	private String ID;
	private String QuestionID;
	private String AnswerID;
	

	//Property ID
	public String getID() { return ID; }
	public void setID(String id) { ID = id; }
	
	//Property QuestionID
	public String getQuestionID(){return QuestionID;}
	public void setQuestionID(String questionID){QuestionID = questionID;}
	
	//Property AnswerID
	public String getAnswerID(){return AnswerID;}
	public void setAnswerID(String answerID){AnswerID = answerID;}
	
	public String createID(int number)
	{
		String result = "CA";
		if(number<10)
		{
			result+="000"+number;
		}
		else if(number<100)
		{
			result+="00"+number;
		}else if(number<1000)
		{
			result+="0"+number;
		}else
		{
			result+=number;
		}
		return result;
	}
	
	public static ArrayList<CorrectAnswerDTO> getCorrectAnswers(Context context, String questionID){
		ArrayList<CorrectAnswerDTO> corectAnswers = new ArrayList<CorrectAnswerDTO>();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	String query = "select * from CorrectAnswers where QuestionID = '"+questionID+"'";
	 	Cursor cursor1 = myDbHelper.getMyDataBase().rawQuery(query, null);
		if(cursor1.moveToFirst()){
			do{
				CorrectAnswerDTO correctAnswer = new CorrectAnswerDTO();
				correctAnswer.setID(cursor1.getString(cursor1.getColumnIndex("ID")));
				correctAnswer.setQuestionID(cursor1.getString(cursor1.getColumnIndex("QuestionID")));
				correctAnswer.setAnswerID(cursor1.getString(cursor1.getColumnIndex("AnswerID")));
				corectAnswers.add(correctAnswer);
			}while(cursor1.moveToNext());
		}
	 	myDbHelper.close();
	 	return corectAnswers;
	}
}
